package com.group1.ws;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.hibernate.Session;

import com.group1.model.Lecturer;
import com.group1.model.Subject;
import com.group1.model.Topic;
import com.group1.model.TopicShortcut;
import com.group1.util.SessionFactoryUtil;

@Path("/topicSC")
public class TopicSCWS {
	@GET
	@Path("/getAll")
	@Produces(MediaType.TEXT_PLAIN)
	public String getAllTopicSC(){
		
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		List<TopicShortcut> list = TopicShortcut.getAllTopicSC(session);
		String result;
		result = "{TOPIC_SC:[";
		boolean first = true;
		for (TopicShortcut t : list) {
			Integer topicSC_ID = t.getTopicshortId();
			Integer topicID = t.getTopic().getTopicId();
			Integer topTopicID = t.getTopTopic().getTopicId();
			String topicSC_Note = t.getTopicshortNote();
			result += (first?"":",");
			first = false;
			result += "{";
			result += "\"topicSC_ID\"" + ":\"" + topicSC_ID + "\",";
			result += "\"topicID\"" + ":\"" + topicID + "\",";
			result += "\"topTopicID\"" + ":\"" + topTopicID + "\",";
			result += "\"topicSC_Note\"" + ":\"" + topicSC_Note + "\",";
			result += "}";
		}
		result += "]}";
		session.getTransaction().commit();
		return result;
	}
	
	@GET
	@Path("/getByTopicSCByID/{topicSC_ID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String getByTopicID(@PathParam("topicSC_ID") String topicSC_ID){
		
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		TopicShortcut topic = TopicShortcut.getToipicSCByID(Integer.valueOf(topicSC_ID), session);
		String result;
		result = "{TOPIC_SC:{";
		result += "\"topicSC_ID\"" + ":\"" + topicSC_ID + "\",";
		result += "\"topicID\"" + ":\"" + topic.getTopic().getTopicId() + "\",";
		result += "\"topTopicID\"" + ":\"" + topic.getTopTopic().getTopicId() + "\",";
		result += "\"topicSC_Note\"" + ":\"" + topic.getTopicshortNote() + "\",";
		result += "}}";
		session.getTransaction().commit();
		return result;
	}
	
	@GET
	@Path("/getBySubjectID/{topicID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String getBySubjectID(@PathParam("topicID") String topicID){
		
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		List<TopicShortcut> list = TopicShortcut.getTopicSCByTopic(Integer.valueOf(topicID), session);
		String result;
		result = "{TOPIC_SC:[";
		boolean first = true;
		for (TopicShortcut t : list) {
			Integer topicSC_ID = t.getTopicshortId();
			Integer topTopicID = t.getTopTopic().getTopicId();
			String topicSC_Note = t.getTopicshortNote();
			result += (first?"":",");
			first = false;
			result += "{";
			result += "\"topicSC_ID\"" + ":\"" + topicSC_ID + "\",";
			result += "\"topicID\"" + ":\"" + topicID + "\",";
			result += "\"topTopicID\"" + ":\"" + topTopicID + "\",";
			result += "\"topicSC_Note\"" + ":\"" + topicSC_Note + "\",";
			result += "}";
		}
		result += "]}";
		session.getTransaction().commit();
		return result;
	}
	
	@GET
	@Path("/getByTopTopic/{topTopicID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String getByTopTopic(@PathParam("topTopicID") String topTopicID){
		
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		List<TopicShortcut> list = TopicShortcut.getTopicSCByTopTopic(Integer.valueOf(topTopicID), session);
		String result;
		result = "{TOPIC_SC:[";
		boolean first = true;
		for (TopicShortcut t : list) {
			Integer topicSC_ID = t.getTopicshortId();
			Integer topicID = t.getTopic().getTopicId();
			String topicSC_Note = t.getTopicshortNote();
			result += (first?"":",");
			first = false;
			result += "{";
			result += "\"topicSC_ID\"" + ":\"" + topicSC_ID + "\",";
			result += "\"topicID\"" + ":\"" + topicID + "\",";
			result += "\"topTopicID\"" + ":\"" + topTopicID + "\",";
			result += "\"topicSC_Note\"" + ":\"" + topicSC_Note + "\",";
			result += "}";
		}
		result += "]}";
		session.getTransaction().commit();
		return result;
	}
	
	
	@GET
	@Path("/create/{topicID}/{topTopicID}/{topicSC_Note}")
	@Produces(MediaType.TEXT_PLAIN)
	public String createTopicSC(@PathParam("topicID") String topicID, @PathParam("topTopicID") String topTopicID,
			@PathParam("topicSC_Note") String topicSC_Note)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 try {
			 	TopicShortcut t = new TopicShortcut();
				t.setTopic(Topic.getTopicByID(Integer.valueOf(topicID), session));
				t.setTopTopic(Topic.getTopicByID(Integer.valueOf(topTopicID), session));
				t.setTopicshortNote(topicSC_Note);
				session.save(t);
				session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			return "error: " + e.getMessage();
		}
		 return "success";
	}
	
	@GET
	@Path("/update/{topicSC_ID}/{topicID}/{topTopicID}/{topicSC_Note}")
	@Produces(MediaType.TEXT_PLAIN)
	public String updateTopic(@PathParam("topicSC_ID") String topicSC_ID, @PathParam("topicID") String topicID,
			@PathParam("topTopicID") String topTopicID, @PathParam("topicSC_Note") String topicSC_Note)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 try {
			 	TopicShortcut t = TopicShortcut.getToipicSCByID(Integer.valueOf(topicSC_ID), session);
				t.setTopic(Topic.getTopicByID(Integer.valueOf(topicID), session));
				t.setTopTopic(Topic.getTopicByID(Integer.valueOf(topTopicID), session));
				t.setTopicshortNote(topicSC_Note);
				session.saveOrUpdate(t);
				session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			return "error: " + e.getMessage();
		}
		 return "success";
	}
	
	@GET
	@Path("/delete/{topicSC_ID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String deleteTopic(@PathParam("topicSC_ID") String topicSC_ID)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 try {
			 session.delete(TopicShortcut.getToipicSCByID(Integer.valueOf(topicSC_ID), session));
			 session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			return "error: " + e.getMessage();
		}
		 return "success";
	}
}
